namespace muduoDotNet
{
    /// <summary>
    /// 事件类型枚举
    /// </summary>
    [Flags]
    public enum EventType
    {
        None = 0,
        ReadEvent = 1 << 0,  // 读事件
        WriteEvent = 1 << 1, // 写事件
        ErrorEvent = 1 << 2  // 错误事件
    }

    /// <summary>
    /// Channel类，负责IO事件的分发
    /// </summary>
    public class Channel : IDisposable
    {
        private readonly EventLoop _eventLoop;
        private int _fd;  // 文件描述符，在C#中可以是Socket的句柄
        private EventType _events;  // 关注的事件
        private EventType _revents;  // 已发生的事件
        private bool _removed;  // 是否已移除
        private Buffer _inputBuffer;  // 输入缓冲区
        private Buffer _outputBuffer; // 输出缓冲区

        public Channel(EventLoop eventLoop, int fd)
        {
            _eventLoop = eventLoop;
            _fd = fd;
            _events = EventType.None;
            _revents = EventType.None;
            AddedToPoller = false;
            _removed = false;
            _inputBuffer = new Buffer();
            _outputBuffer = new Buffer();
        }

        public int Fd => _fd;
        public EventType Events => _events;
        public EventType Revents
        {
            get => _revents;
            set => _revents = value;
        }
        public bool AddedToPoller { get; private set; }
        public bool Removed => _removed;

        /// <summary>
        /// 设置是否已添加到Poller（内部使用）
        /// </summary>
        /// <param name="added">是否已添加</param>
        internal void SetAddedToPoller(bool added)
        {
            AddedToPoller = added;
        }

        // 事件回调
        public Action ReadCallback { get; set; }
        public Action WriteCallback { get; set; }
        public Action ErrorCallback { get; set; }

        /// <summary>
        /// 获取输入缓冲区
        /// </summary>
        /// <returns>输入缓冲区</returns>
        public Buffer GetInputBuffer() => _inputBuffer;

        /// <summary>
        /// 获取输出缓冲区
        /// </summary>
        /// <returns>输出缓冲区</returns>
        public Buffer GetOutputBuffer() => _outputBuffer;

        /// <summary>
        /// 处理事件
        /// </summary>
        public void HandleEvent()
        {
            if (_removed)
            {
                return;
            }

            // 处理错误事件
            if ((_revents & EventType.ErrorEvent) != 0)
            {
                ErrorCallback?.Invoke();
            }

            // 处理读事件
            if ((_revents & EventType.ReadEvent) != 0)
            {
                ReadCallback?.Invoke();
            }

            // 处理写事件
            if ((_revents & EventType.WriteEvent) != 0)
            {
                WriteCallback?.Invoke();
            }
        }

        /// <summary>
        /// 启用读事件
        /// </summary>
        public void EnableReading()
        {
            _events |= EventType.ReadEvent;
            Update();
        }

        /// <summary>
        /// 禁用读事件
        /// </summary>
        public void DisableReading()
        {
            _events &= ~EventType.ReadEvent;
            Update();
        }

        /// <summary>
        /// 启用写事件
        /// </summary>
        public void EnableWriting()
        {
            _events |= EventType.WriteEvent;
            Update();
        }

        /// <summary>
        /// 禁用写事件
        /// </summary>
        public void DisableWriting()
        {
            _events &= ~EventType.WriteEvent;
            Update();
        }

        /// <summary>
        /// 禁用所有事件
        /// </summary>
        public void DisableAll()
        {
            _events = EventType.None;
            Update();
        }

        /// <summary>
        /// 检查是否正在关注读事件
        /// </summary>
        public bool IsReading()
        {
            return (_events & EventType.ReadEvent) != 0;
        }

        /// <summary>
        /// 检查是否正在关注写事件
        /// </summary>
        public bool IsWriting()
        {
            return (_events & EventType.WriteEvent) != 0;
        }

        /// <summary>
        /// 更新Channel在Poller中的注册
        /// </summary>
        private void Update()
        {
            // 实际应用中，这里应该更新Poller中的事件注册
            // 在当前实现中，我们假设EventLoop有一个Poller成员，这里简化处理
            // 实际代码中应该调用Poller的UpdateChannel方法
            _eventLoop.RunInLoop(() => _eventLoop.UpdateChannel(this));
        }

        /// <summary>
        /// 从Poller中移除Channel
        /// </summary>
        public void Remove()
        {
            if (!_removed)
            {
                _removed = true;
                SetAddedToPoller(false);
                _events = EventType.None;
                
                // 实际应用中，这里应该调用Poller的RemoveChannel方法
                _eventLoop.RunInLoop(() => _eventLoop.RemoveChannel(this));
            }
        }

        public void Dispose()
        {
            Remove();
        }
    }
}